package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.controller.form.UpdateAccountPassForm;
import com.woniuxy.controller.info.AccountInfo;
import com.woniuxy.model.Account;
import com.woniuxy.model.PawnGoods;
import com.woniuxy.service.AccountService;
import com.woniuxy.service.PawnGoodsService;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.commons.Acc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Api(tags = "用户信息相关接口")
@RestController
@RequestMapping("/account")
public class AccountController {


    @Resource
    private AccountService accountService;

    @ApiOperation("用户退出登录")
    @RequestMapping("/loginerror")
    @ResponseBody
    public Result loginError(){
        return Result.success(403,"未登录",null);
    }

    @ApiOperation("修改密码")
    @RequestMapping("/updatePassword")
    @ResponseBody
    public Result updatePassword(UpdateAccountPassForm updateAccountPassForm){
        String password = new BCryptPasswordEncoder().encode(updateAccountPassForm.getAccountPass());
        Account account = new Account();
        account.setAccountId(updateAccountPassForm.getAccountId());
        account.setAccountPassword(password);
        UpdateWrapper<Account> uw = new UpdateWrapper<>();
        uw.eq("account_id",account.getAccountId());
        accountService.update(account,uw);
        return Result.success(200,"成功修改",null);
    }

    @ApiOperation("获取所有用户")
    @GetMapping("/getAllAccount")
    @ResponseBody
    @Acc
    public Result getAllAccount(){
        List<Account> list = accountService.list();
        List<AccountInfo> accountInfos = NewCopyBean.copyList(list, AccountInfo.class);
        return Result.success(200,"查询成功",accountInfos);
    }

}

